<template>
  <div class="container">
    <!-- 添加目录对话框 -->
    <el-dialog
      :title="dirID?'修改目录':'新增目录'"
      :visible="dialogVisible"
      width="30%"
      @close="closedirectorysDialog"
      @open="oepndirectorysDialog"
    >
      <!-- <span>这是一段信息</span> -->
     <el-form label-width="100px" :model="dirForm" :rules="dirRules" ref="dirRef">
      <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
        <el-select v-model="dirForm.subjectID" style="width:100%" placeholder="请选择">
          <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="dirForm.directoryName" style="width:100%" placeholder="请输入目录名称" />
      </el-form-item>
     </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closSubmit">取 消</el-button>
        <el-button type="primary" @click="submit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    dirID: {
      type: Number,
      required: true
    }
    // params: {
    //   type: Object,
    //   required: true
    // }

  },
  data () {
    return {
      dirForm: {
        subjectID: '',
        directoryName: ''
      },
      subjectList: [],
      dirRules: {
        directoryName: [
          { required: true, message: '目录不能为空', trigger: ['blur'] }
        ],

        subjectID: [
          { required: true, message: '请选择所属学科', trigger: ['blur'] }
        ]
      }
    }
  },
  methods: {
    closedirectorysDialog () {
      this.$emit('input')
      this.$refs.dirRef.resetFields()
    },
    closSubmit () {
      this.$emit('input')
    },
    async oepndirectorysDialog () {
      const res = await simple()
      console.log('返回来的数据', res.data)
      this.subjectList = res.data
    },
    submit () {
      this.$refs.dirRef.validate(async valid => {
        if (!valid) return
        // 效验通过
        // 发起请求
        if (this.dirID) {
          await update({ id: this.dirID, ...this.dirForm })
          this.$emit('search')
        } else {
          if (this.$route.query.id) {
            await add({
              subjectID: this.$route.query.id,
              directoryName: this.dirForm.directoryName
            })
            // this.
          } else {
            const res = await add(this.dirForm)
            console.log('数据', res)
            // this.$emit('Page')
          }
          this.$emit('Page')
          this.$emit('listForm')
        }
        const msg = this.dirID ? '修改目录成功' : '新增目录成功'
        this.$message.success(msg)
        // 关闭弹窗
        this.$emit('input')
        // 刷新表格数据

        // if (this.dirForms.directoryName || this.dirForms.state >= 0) {
        //   this.$emit('search', this.dirForm)
        // } else {
        //   this.$emit('listForm', this.dirForm)
        // }
      })
    },
    async dirText (id) {
      const res = await detail({ id: id })
      this.dirForm = res.data
    }
  }
}
</script>

<style scoped lang="less"></style>
